//
//  WFAddSavingsMethodTextCell.m
//  WeiFang
//
//  Created by WeiFang Team on 2023.
//  Copyright © 2023 WeiFang Team. All rights reserved.
//

#import "WFAddSavingsMethodTextCell.h"
#import <objc/runtime.h>

@implementation WFAddSavingsMethodTextCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _textField.delegate = self;

    Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *minPlaceholderLabel = object_getIvar(_textField, ivar);
    minPlaceholderLabel.textColor = UIColorWithHex(0x999999);
    minPlaceholderLabel.font = [UIFont systemFontOfSize:16];
}

- (void)setData:(id)data delegate:(id)delegate {
    
    _delegate = delegate;
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[WFAddSavingsMethodTextCellModel class]]) {
            WFAddSavingsMethodTextCellModel *model = data[@"data"];
            _model = model;
            _titleLabel.text = model.title;
            _textField.text = model.content;
            
            if ([model.title isEqualToString:@"开始值"]) {
                _textField.placeholder = @"请输入1-1000的数值";
            } else if ([model.title isEqualToString:@"每天增加"]) {
                _textField.placeholder = @"请输入1-1000的数值";
            }
        }
    }
}

- (IBAction)textFieldChange:(UITextField *)sender {
    if (sender.text.integerValue <= 0) {
        _textField.text = @"";
    } else if (sender.text.integerValue > 1000) {
        _textField.text = @"1000";
    }
    if (_delegate && [_delegate respondsToSelector:@selector(addSavingsMethodTextCell:textField:)]) {
        [_delegate addSavingsMethodTextCell:self textField:_textField];
    }
}

- (IBAction)textFieldBegin:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(addSavingsMethodTextCell:textField:)]) {
        [_delegate addSavingsMethodTextCell:self textField:sender];
    }
}

@end


#pragma mark - Model

@implementation WFAddSavingsMethodTextCellModel

+ (WFAddSavingsMethodTextCellModel *)ittemModelWithTitle:(NSString *)title content:(NSString *)content {
    WFAddSavingsMethodTextCellModel *model = [WFAddSavingsMethodTextCellModel new];
    model.title = title.length > 0 ? title : @"";
    model.content = content.length > 0 ? content : @"";
    return model;
}

@end
